# Generic Makefile 
# Written by Emilien Dupont (~edupont)
# Feel free to share it !

# Cause every Makefile should contain this line.
SHELL=/bin/sh
# Change it to whatever you want to use release mode instead of debug mode.
DEBUG=yes
# Command to invoke the compiler.
CC=gcc
# Extension of your source files.
EXT_SRC=.c
# Extension of your object files.
EXT_OBJ=.o
ifeq ($(DEBUG), yes)
	# C flags for debug mode.
	CFLAGS=-W -Wall -g -std=c99
	# Linker flags for debug mode.
	LDFLAGS=-lpthread
else
	# C flags for release mode.
	CFLAGS=-W -Wall -ansi -O3 -std=c99
	# Linker flags for release mode.
	LDFLAGS=-lpthread
endif
# Name of the executable file.
EXEC=test
# List of the source files.
SRC=$(wildcard *$(EXT_SRC))
# List of the object files.
OBJ=$(SRC:$(EXT_SRC)=$(EXT_OBJ))
# Installation directory
INSTALL_DIR=

all: $(EXEC) 
ifeq ($(DEBUG),yes)
	@echo "\033[32mGenerated in debug mode\033[0m"
else
	@echo "\033[32mGenerated in realease mode\033[0m"
endif		

$(EXEC): $(OBJ)
	@$(CC) -o $@ $^ $(LDFLAGS)
	@echo "\033[33mLinking ...\033[0m"

%$(EXT_OBJ): %$(EXT_SRC)
	@echo "\033[33mCompiling "$<"...\033[0m"
	@$(CC) -o $@ -c $< $(CFLAGS)

install:
ifdef INSTALL_DIR
	@cp -v $(EXEC) $(INSTALL_DIR)
endif

.PHONY: clean mrproper

clean:
	@rm -fv *$(EXT_OBJ)

mrproper: clean
	@rm -fv $(EXEC)

